package eu.tolu.cableSpotter;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import eu.tolu.cableSpotter.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Address;
import android.location.Geocoder;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class SpotView extends Activity {
	public String address = "";
	public String cableName = "";
	public String cablePhone = "";
	public String cableEmail = "";
	public String cableURL = "";
	public String isCustome = "";
	
    public void onNavButtonClick(final View view) {
    	switch (view.getId()) {
    	case R.id.bt_id_sview_call:
    		System.out.println("CALL TO :"+this.cablePhone);
    		
    		Intent callIntent = new Intent(Intent.ACTION_DIAL,
    				Uri.parse("tel:"+this.cablePhone));
    		startActivity(callIntent);
    		break;
    	case R.id.bt_id_sview_email:
    		Intent emailIntent = new Intent(Intent.ACTION_VIEW,
    				Uri.parse("mailto:"+this.cableEmail));
    		startActivity(emailIntent);
    		break;
    	case R.id.bt_id_sview_web:
    		Intent webIntent = new Intent(Intent.ACTION_VIEW,
    				Uri.parse(this.cableURL));
    		startActivity(webIntent);
    		break;
    	case R.id.bt_gmap_view:
    		Intent gMapIntent = new Intent(this, GmapView.class);
    		gMapIntent.putExtra("address", address);
    		gMapIntent.putExtra("cableName", cableName);
    		startActivity(gMapIntent);
    		break;
    	case R.id.bt_id_sview_edit:
    		Intent editCableIntent = new Intent(this, AddCable.class);
    		editCableIntent.putExtra("cableName", cableName);
    		startActivity(editCableIntent);
    		break;
    	}
    }

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spot_view);

		final Bundle extras = getIntent().getExtras();

		if (extras != null) {
			final TextView txCabName = (TextView) findViewById(R.id.tx_cable_name);
			txCabName.setText(extras.getString("location"));
			cableName=extras.getString("location");

			final TextView txCabCountry = (TextView) findViewById(R.id.tx_cable_country);
			txCabCountry.setText("Country: "+extras.getString("country"));

			final TextView txCabAdd = (TextView) findViewById(R.id.tx_cable_address);
			txCabAdd.setText("Address: "+extras.getString("add1")+"\n"+extras.getString("add2")+"\n"+extras.getString("add3"));

			final TextView txCabPhone = (TextView) findViewById(R.id.tx_cable_phone);
			txCabPhone.setText("Phone: "+extras.getString("phone"));
			cablePhone = extras.getString("phone");

			final TextView txCabFax = (TextView) findViewById(R.id.tx_cable_fax);
			txCabFax.setText("Fax: "+extras.getString("fax"));

			final TextView txCabEmail = (TextView) findViewById(R.id.tx_cable_email);
			txCabEmail.setText("Email: "+extras.getString("email"));
			cableEmail = extras.getString("email");

			final TextView txCabWeb = (TextView) findViewById(R.id.tx_cable_web);
			txCabWeb.setText("Web: "+extras.getString("web"));
			cableURL = extras.getString("web");
			
			isCustome = extras.getString("custome");
			
//			final TextView txtFrage4 = (TextView) findViewById(R.id.tx_cable_image);
//			txtFrage4.setText(extras.getString("image"));
	        
			
			if(extras.getString("img")!= null && extras.getString("img") != ""){
				new DownloadImageTask((ImageView) findViewById(R.id.myImageView))
				.execute(extras.getString("img"));
			}else{
				ImageView noimage = (ImageView) findViewById(R.id.myImageView);
				noimage.setImageResource(R.drawable.no_photo);
			}
			
			address = extras.getString("country")+", "+extras.getString("add2")+", "+extras.getString("add3");
		}
		
		Button editButton = (Button) findViewById(R.id.bt_id_sview_edit);
		if(isCustome == null || isCustome.equals(""))
			editButton.setVisibility(View.GONE);
	}
}

class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
    	if(result != null)
    		bmImage.setImageBitmap(result);
    	else {
    		bmImage.setImageResource(R.drawable.no_photo);
		}
    }
}
